//
//  SelectedGoodsCell.m
//  SeplitDemo
//
//  Created by 信恩世通 on 2017/8/1.
//  Copyright © 2017年 Xiyuekeji. All rights reserved.
//

#import "SelectedGoodsCell.h"

#import "GoodsModel.h"


#define discountSales 0.1
@interface SelectedGoodsCell ()

@property (weak, nonatomic) UIImageView *iconView;
@property (weak, nonatomic) UILabel *nameLab;
@property (weak, nonatomic) UILabel *buyCountLab;
@property (weak, nonatomic) UILabel *totalCostLab;
@property (weak, nonatomic) UILabel *salesLab;
@property (weak, nonatomic) UILabel *discountsLab;
@property (weak, nonatomic) UILabel *discountsCostLab;

@end

@implementation SelectedGoodsCell


#pragma mark - **** instance ****

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        UIImageView *iconView = [[UIImageView alloc] initWithFrame:CGRectZero];
        iconView.contentMode = UIViewContentModeScaleToFill;
        
        [self addSubview:iconView];
        self.iconView = iconView;
        
        [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self).offset(5.0f);
            make.left.mas_equalTo(self).offset(5.0f);
            make.width.height.mas_equalTo(50);
        }];
        
        UILabel *nameLab = [UILabel infoLabel];
        nameLab.numberOfLines = 2;
        nameLab.lineBreakMode = NSLineBreakByWordWrapping;
        
        [self addSubview:nameLab];
        self.nameLab = nameLab;
        
        [nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self).offset(5.0f);
            make.left.mas_equalTo(iconView.mas_right).offset(5.0f);
            make.right.mas_equalTo(self).offset(-20.0f);
        }];
        
        UILabel *totalCostLab = [UILabel infoLabel];
        totalCostLab.font = Font(12);
        totalCostLab.textColor = COLOR_TEXT_GARY;
        
        [self addSubview:totalCostLab];
        self.totalCostLab = totalCostLab;
        
        [totalCostLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(nameLab.mas_bottom).offset(5.0f);
            make.right.mas_equalTo(self).offset(-SPACE);
            make.width.mas_equalTo(50.0f);
        }];
        
        UILabel *buyCountLab = [UILabel infoLabel];
        buyCountLab.font = Font(12);
        buyCountLab.textColor = COLOR_TEXT_GARY;
        
        [self addSubview:buyCountLab];
        self.buyCountLab = buyCountLab;
        
        [buyCountLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(totalCostLab.mas_top).offset(5.0f);
            make.left.mas_equalTo(iconView.mas_right).offset(SPACE);
            make.right.mas_equalTo(totalCostLab.mas_left);
        }];
        
        UILabel *salesLab = [UILabel infoLabel];
        salesLab.font = Font(12);
        salesLab.textColor = COLOR_TEXT_GARY;
        
        [self addSubview:salesLab];
        self.salesLab = salesLab;
        
        [salesLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(buyCountLab.mas_bottom).offset(5.0f);
            make.left.mas_equalTo(iconView.mas_right).offset(SPACE);
            make.right.mas_equalTo(totalCostLab.mas_right);
        }];
        
        UILabel *discountsCostLab = [UILabel infoLabel];
        discountsCostLab.font = Font(12);
        discountsCostLab.textColor = COLOR_TEXT_GARY;
        
        [self addSubview:discountsCostLab];
        self.discountsCostLab = discountsCostLab;
        
        [discountsCostLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(salesLab.mas_bottom).offset(5.0f);
            make.right.mas_equalTo(self).offset(-SPACE);
            make.width.mas_equalTo(50.0f);
        }];
        
        UILabel *discountsLab = [UILabel infoLabel];
        discountsLab.font = Font(12);
        discountsLab.textColor = COLOR_TEXT_GARY;
        
        [self addSubview:discountsLab];
        self.discountsLab = discountsLab;
        
        [discountsLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(salesLab.mas_bottom).offset(5.0f);
            make.left.mas_equalTo(iconView.mas_right).offset(SPACE);
            make.right.mas_equalTo(discountsCostLab.mas_right);
            make.bottom.mas_equalTo(self.mas_bottom).offset(-SPACE);
        }];
        
    }
    
    return self;
}

#pragma mark - **** setter & getter ****

- (void)setGoodsItem:(GoodsModel *)goodsItem {
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:goodsItem.ProductImg] placeholderImage:[UIImage imageNamed:@"default60"]];
    self.nameLab.text = goodsItem.ProductName.length >= 30 ? [goodsItem.ProductName substringToIndex:30] : goodsItem.ProductName;
    self.buyCountLab.text = [NSString stringWithFormat:@"%ld X $%.2f",goodsItem.buyCount,[goodsItem.Price floatValue]];
    self.totalCostLab.text = [NSString stringWithFormat:@"$%.2f",[goodsItem.Price floatValue] * goodsItem.buyCount];
    self.salesLab.text = @"Scales:Raven Y";
    self.discountsCostLab.text = [NSString stringWithFormat:@"$%.2f",[goodsItem.Price floatValue] * goodsItem.buyCount * discountSales];
    self.discountsLab.text = [NSString stringWithFormat:@"summer sales(%.2f%%)",discountSales*100];
}


@end
